package com.bwf.nio;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * @Version v1.0
 * @Description: TODO
 * @Author ZHANZHONGXIN
 * @Date 2022/5/18 14:56
 * @Copyright 博为峰
 */
public class PathDemo {
    /*
       1.注册
         卡号
         密码
         余额
       2.创建对象，持久化到文件中，文件名用户卡号
       3.存储的功能重写改一下，卡号，密码
         遍历文件夹account/a1.txt,b1.txt



     */
    public static void main(String[] args) throws IOException {
//        m1();
//        m2();
//        m3();
//        m4();
        //Path-->File
        Path path = Paths.get("D:\\works22\\java22\\JAVA.md");
        File file = path.toFile();

        //File-->Path
        file = new File("D:\\works22\\java22\\JAVA.md");
        path = file.toPath();

    }

    private static void m4() throws IOException {
        Path path = Paths.get("C:\\Users\\admin\\Desktop\\io");
        Files.walkFileTree(path, new FileVisitor() {
            //访问前
            @Override
            public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问前preVisitDirectory:" + dir);
                return FileVisitResult.CONTINUE;
            }

            //访问时
            @Override
            public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException {
                System.out.println("访问时visitFile:" + file);
                return FileVisitResult.CONTINUE;
            }

            //访问失败时
            @Override
            public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }

            //访问后
            @Override
            public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException {
                System.out.println("访问后postVisitDirectory:" + dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }

    private static void m3() throws IOException {
        Path path = Paths.get("a.txt");
        Path target = Paths.get("a2.txt");
//        Files.copy(path, target, StandardCopyOption.REPLACE_EXISTING);
        Files.delete(target);
    }

    private static void m2() throws IOException {
        Path path = Paths.get("a.txt");
        BufferedWriter bufferedWriter = Files.newBufferedWriter(path);
        bufferedWriter.write("abc");
        bufferedWriter.close();
    }

    private static void m1() throws IOException {
        Path path = Paths.get("D:\\works22\\java22\\JAVA.md");
        System.out.println(Files.getLastModifiedTime(path));//修改时间
        System.out.println(Files.size(path));//文件大小
    }
}
